#!/bin/bash

########## attention ###############
#1. change ARCH_BIN, CUDNN version
#2. change opencv unzip location(need tar -xf first)
#3. change python version(PYTHON_VERSION)
#4. change python env location(OPENCV_INSTALL_LOCATION)

#notify:
# 1. need comfirm python interpreter is not None, else stop once and delete opencv-version/build, and re-config the const
# 2. maybe you should change the link when GLIBCXX_xxx error is show
# 3. opencv: https://github.com/opencv/opencv/archive/refs/tags/4.5.4.tar.gz
# 4. opencv_contrib: https://github.com/opencv/opencv_contrib/archive/refs/tags/4.5.4.tar.gz
####################################


set -euf -o pipefail


OPENCV_VERSION=4.5.4  # OpenCV defines
ARCH_BIN=8.6  # Jetson AGX Orin is 8.7, other need looking from nvidia
CUDNN_VERSION=8.4
OPENCV_DOWNLOAD_DIR=/home/tacom/code/opencv/downloads
PYTHON_VERSION=3.7
OPENCV_INSTALL_LOCATION=/home/tacom/envs/opencv_4.5.4

# download
NUM_JOBS=8
OPENCV_DIR=opencv-$OPENCV_VERSION
OPENCV_CONTRIB_DIR=opencv_contrib-$OPENCV_VERSION
OPENCV_BUILD_DIR=$OPENCV_DIR/build
OPENCV_GITHUB_REPO=https://github.com/opencv
OPENCV_REPO=$OPENCV_GITHUB_REPO/opencv/archive/refs/tags/$OPENCV_VERSION.tar.gz
OPENCV_CONTRIB_REPO=$OPENCV_GITHUB_REPO/opencv_contrib/archive/refs/tags/$OPENCV_VERSION.tar.gz

# for python env or system global
PYTHON_DEFAULT_EXECUTABLE=$OPENCV_INSTALL_LOCATION/bin/python
PYTHON3_EXECCUTABLE=$OPENCV_INSTALL_LOCATION/bin/python
PYTHON3_INCLUDE_DIR=$OPENCV_INSTALL_LOCATION/include/python${PYTHON_VERSION}m
PYTHON3_PACKAGES_PATH=$OPENCV_INSTALL_LOCATION/lib/python${PYTHON_VERSION}/site-packages
PYTHON3_LIBRARY=$OPENCV_INSTALL_LOCATION/lib/libpython${PYTHON_VERSION}m.so

# Compile and Install OpenCV with CUDA
echo "Downloading and compiling OpenCV with CUDA support"

# for jetson orin env
#sudo apt install -y cmake libavcodec-dev libavformat-dev libavutil-dev libeigen3-dev libglew-dev libgtk2.0-dev libgtk-3-dev libjpeg-dev libpng-dev libpostproc-dev libswscale-dev libtbb-dev libtiff5-dev libv4l-dev libxvidcore-dev libx264-dev qt5-default zlib1g-dev python-dev python-numpy python-py python-pytest python3-dev python3-py python3-pytest libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev    

# normal pc
sudo apt install -y build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev python-dev-is-python3 libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff5-dev


cd $OPENCV_DOWNLOAD_DIR

# Create the build directory and start cmake
if [ ! -d $OPENCV_DOWNLOAD_DIR/$OPENCV_BUILD_DIR ]
then
  mkdir $OPENCV_DOWNLOAD_DIR/$OPENCV_BUILD_DIR
fi

cd $OPENCV_DOWNLOAD_DIR/$OPENCV_BUILD_DIR


# -D PYTHON_DEFAULT_EXECUTABLE=${PYTHON_DEFAULT_EXECUTABLE} \
# -D PYTHON3_EXECUTABLE=${PYTHON3_EXECCUTABLE} \
# -D PYTHON3_INCLUDE_DIR=${PYTHON3_INCLUDE_DIR} \
# -D PYTHON3_PACKAGES_PATH=${PYTHON3_PACKAGES_PATH} \
# -D PYTHON3_LIBRARY=${PYTHON3_LIBRARY} \

cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=${OPENCV_INSTALL_LOCATION} \
      -D WITH_CUDA=ON \
      -D WITH_CUDNN=ON \
      -D CUDNN_VERSION=${CUDNN_VERSION} \
      -D CUDA_ARCH_BIN=${ARCH_BIN} \
      -D CUDA_ARCH_PTX="" \
      -D ENABLE_FAST_MATH=ON \
      -D CUDA_FAST_MATH=ON \
      -D WITH_CUBLAS=ON \
      -D WITH_LIBV4L=ON \
      -D WITH_V4L=ON \
      -D WITH_GSTREAMER=ON \
      -D WITH_GSTREAMER_0_10=OFF \
      -D WITH_QT=OFF \
      -D WITH_GTK=ON \
      -D WITH_GTK_2_X=ON \
      -D WITH_OPENGL=ON \
      -D BUILD_opencv_python2=ON \
      -D BUILD_opencv_python3=ON \
      -D BUILD_TIFF=ON \
      -D BUILD_TESTS=OFF \
      -D BUILD_PERF_TESTS=OFF \
      -D OPENCV_GENERATE_PKGCONFIG=YES \
      -D OPENCV_EXTRA_MODULES_PATH=../../${OPENCV_CONTRIB_DIR}/modules \
      -D CPACK_PACKAGE_VERSION=${OPENCV_VERSION} \
      -D EXTRAMODULES_VCSVERSION=${OPENCV_VERSION} \
      -D OPENCV_VCSVERSION=${OPENCV_VERSION} \
      ../
      make -j${NUM_JOBS}
      make install
      ldconfig

echo "OpenCV installed in: $OPENCV_INSTALL_LOCATION"
